﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AteneoLibriBusinessLogic.Entities;

namespace AteneoLibri.Account
{
    public partial class ChangePersonalInfo : System.Web.UI.Page
    {
        User user = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            pnlUpdateSuccessfullyCompleted.Visible = false;
            pnlUpdateError.Visible = false;
            
            if (!Page.IsPostBack)
            {
                user = Utility.HttpSessionManager.GetWebUser();

                if (user != null)
                {
                    using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                    {
                        // load universities
                        List<University> universities = service.getUniversities();
                        ddlUniversity.DataSource = universities;
                        ddlUniversity.DataBind();

                        // load all user data and bind data into form
                        txtName.Text = user.Name;
                        txtSurname.Text = user.Surname;
                        txtEmail.Text = user.Email;
                        txtUsername.Text = user.Username;

                        List<Faculty> faculties = null;

                        if (user.UniversityId != null)
	                    {
                            ddlUniversity.SelectedValue = user.UniversityId.ToString();
                            faculties = service.getFacultiesByUniversity(user.UniversityId.Value);
                            ddlFaculty.DataSource = faculties;
                            ddlFaculty.DataBind();
	                    }
                        

                        if (faculties != null && user.FacultyId != null)
                        {
                            // load the user faculty
                            ddlFaculty.SelectedValue = user.FacultyId.ToString();
                        }

                    }
                }
                else
                {
                    // Redirect per sessione scaduta
                    Response.Redirect("~/Logout.aspx");
                }
                
            }
        }

        protected void ddlUniversity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetFacultyDropDown();

            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                var universityId = ddlUniversity.SelectedItem.Value;
                if (universityId != "0")
                {
                    List<Faculty> faculties = service.getFacultiesByUniversity(new Guid(universityId));
                    ddlFaculty.DataSource = faculties;
                    ddlFaculty.DataBind();
                }
            }
        }

        private void ResetFacultyDropDown()
        {
            ddlFaculty.Items.Clear();
            ddlFaculty.Items.Add("-- Seleziona --");
            ddlFaculty.Items[0].Selected = true;
            ddlFaculty.AppendDataBoundItems = true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    User modifiedUser = Utility.HttpSessionManager.GetWebUser();

                    cvUser.IsValid = !service.CheckExistingRegistration(txtEmail.Text.ToLower().Trim()) || (modifiedUser.Email.Equals(txtEmail.Text.ToLower().Trim()));
                    cvUserName.IsValid = !service.UserNameExist(txtUsername.Text.ToLower().Trim()) || (modifiedUser.Username.Equals(txtUsername.Text.ToLower().Trim()));  // Check if the username already exist!

                    if (Page.IsValid)
                    {
                        modifiedUser.Name = txtName.Text;
                        modifiedUser.Surname = txtSurname.Text;
                        modifiedUser.Email = txtEmail.Text;
                        modifiedUser.Username = txtUsername.Text;

                        if (ddlUniversity.SelectedIndex != 0)
                        {
                            modifiedUser.UniversityId = new Guid(ddlUniversity.SelectedValue);

                            if (ddlFaculty.SelectedIndex != 0)
                            {
                                modifiedUser.FacultyId = new Guid(ddlFaculty.SelectedValue);
                            }
                        }

                        bool result = service.updateUser(modifiedUser);
                        if (result)
                        {
                            // open panel
                            pnlUpdateSuccessfullyCompleted.Visible = true;
                            pnlUpdate.Visible = false;
                        }
                    }
                    else
                    {
                        pnlUpdateError.Visible = true;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                // throw;
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dashboard.aspx");
        }
    }
}